﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using EngineOfEvermore.Common;
using System.Diagnostics;

namespace EngineOfEvermore.Imaging.AgateImplementation
{
    public class AgateRenderer : IRenderer
    {
        IReadonlyCamera _camera;

        public AgateRenderer( Camera camera )
        {
            _camera = camera;
            this.Scale = new Vector2d( 1, 1 );
            this.Color = Color.White;
        }

        public IReadonlyCamera Camera
        {
            get { return _camera; }
        }

        public ImageMap ImageMap
        {
            get;
            set;
        }

        public Vector2d Scale
        {
            get;
            set;
        }

        public float Rotation
        {
            get;
            set;
        }

        public System.Drawing.Color Color
        {
            get;
            set;
        }

        public bool FlipX
        {
            get;
            set;
        }

        public bool FlipY
        {
            get;
            set;
        }

        public void DrawFrame( int frame, Vector2d position )
        {
            Debug.Assert( this.ImageMap != null );

            if ( Rotation.EpsilonIsZero() )
            {
                this.ImageMap.DrawFrame( frame, position, this.Scale, this.Color, this.FlipX,
                    this.FlipY );
            }
            else
            {
                this.ImageMap.DrawFrameRotated( frame, position, this.Rotation, this.Scale,
                    this.Color, this.FlipX, this.FlipY );
            }
        }

        public void DrawFrame( int frame, Vector2d position, RectangleF localClip )
        {
            this.ImageMap.DrawFrameClippedAndRotated( frame, position, this.Rotation, this.Scale, this.FlipX,
                this.FlipY, this.Color, localClip );
        }

        public void DrawFrame( int frame, RectangleF destRect )
        {
            this.ImageMap.DrawFrameInRectangle( frame, destRect, this.Color, this.FlipX,
                this.FlipY );
        }

        public void DrawRect( RectangleF rect )
        {
            var color = AgateLib.WinForms.Interop.Convert( this.Color );
            var transformedRect = AgateLib.WinForms.Interop.Convert( rect );
            AgateLib.DisplayLib.Display.DrawRect( transformedRect, color );
        }

        public void DrawLine( Vector2d begin, Vector2d end )
        {
            AgateLib.DisplayLib.Display.DrawLine( (int) begin.X, (int) begin.Y, (int) end.X,
                (int) end.Y, AgateLib.WinForms.Interop.Convert( this.Color ) );
        }

        public void DrawCircle( Vector2d position, float radius )
        {
            var rect = new AgateLib.Geometry.Rectangle(
                (int) ( position.X - radius + 0.5f ),
                (int) ( position.Y - radius + 0.5f ),
                (int) ( 2 * radius + 0.5f ),
                (int) ( 2 * radius + 0.5f ) );

            AgateLib.DisplayLib.Display.DrawEllipse( rect,
                AgateLib.WinForms.Interop.Convert( this.Color ) );
        }
    }
}
